﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Safe_User_inc.aspx.cs" Inherits="newBlog.CenterInc.view_inc.Safe_User_inc" %>

<%@ Import Namespace="newBlog.CenterInc.tool" %>
<%@ Import Namespace="newBlog.CenterInc.Ctrltor" %>
<%  Response.ContentType = "application/json";
    if (Session["user_id"] == null)
    {
        Response.Write("{\"error\":\"请登录\"}");
        return;
    }
    int mode = (Request.QueryString["mode"] == null || Request.QueryString["mode"].Length == 0) ? 0 : Convert.ToInt32(Request.QueryString["mode"]);//1修改密码 2修改邮箱
    isString isString = new isString();
    User_inc user_Inc = new User_inc();
    string EmailVer  ;
    switch (mode)
    {
        case 1:
            string oldPassword = Request.Form["oldPassword"];
            string newPassword = Request.Form["newPassword"];
            EmailVer = Request.Form["EmailVer"];
            string SafeUserVer = Session["SafeUserVer"]==null?null:Session["SafeUserVer"].ToString();
            if (isString.isEmpty(oldPassword, newPassword, EmailVer))
            {
                Response.Write("{\"error\":\"不允许留空哦~\"}");
                return;
            }
            if (SafeUserVer == null)
            {
                Response.Write("{\"error\":\"请获取验证码~\"}");
                return;
            }

            if (isString.IsRange(5, 20, newPassword))
            {
                Response.Write("{\"error\":\"新密码需要在5~20个字符之间\"}");
                return;
            }
            if (isString.IsChar(@"^[^0-9a-z._]+$", newPassword))
            {
                Response.Write("{\"error\":\"密码不能包含特殊字符哦...\"}");
                return;
            }
            if (!SafeUserVer.Equals(EmailVer))
            {
                Response.Write("{\"error\":\"验证码不正确\"}");
                return;
            }
            if (user_Inc.SelectedUser(Session["user_name"].ToString(), oldPassword)<1) {
                Response.Write("{\"error\":\"原密码不正确\"}");
                return;
            }
            if (user_Inc.SelectedUser(Session["user_name"].ToString(), newPassword) > 0)
            {
                Response.Write("{\"error\":\"新的密码不能与原密码相同\"}");
                return;
            }

            if (user_Inc.UpPwd(Session["user_id"].ToString(), newPassword))
            {
                Response.Write("{\"error\":\"修改失败\"}");
                return;
            }
            Session.Remove("SafeUserVer");
            Response.Write("{\"msg\":\"修改成功\"}");
            return;
        case 2:
            string newEmail = Request.Form["newEmail"];
            EmailVer = Request.Form["EmailVer"];
            string safeEmailVer=Session[newEmail+"EmailVer"]==null?null:Session[newEmail+"EmailVer"].ToString();
            if (isString.isEmpty(newEmail, EmailVer))
            {
                Response.Write("{\"error\":\"不允许留空哦~\"}");
                return;
            }
            if (safeEmailVer == null)
            {
                Response.Write("{\"error\":\"请获取验证码~\"}");
                return;
            }
            
            if (!safeEmailVer.Equals(EmailVer)) {
                Response.Write("{\"error\":\"验证码错误~\"}");
                return;
            }
            if (user_Inc.upemail(Session["user_id"].ToString(),newEmail)) { 
                Response.Write("{\"error\":\"修改失败~\"}");
                return;
            }
            Session.Remove(newEmail+"EmailVer");
            Response.Write("{\"msg\":\"修改成功\"}");
            return;

    }
%>